<?php

class AppModel extends Model {
	
	function _isUnique($check, $field) {
		/**
		 * Don't work for now; cake 1.1.17 don't accept array in $validate
		 * 
		 * script from http://walkerhamilton.com/465/validate-uniqueness-in-cakephp
		 * 
		 * usage (in any model) :
		 	var $validate = array(
		 		'email' =>array('rule' => array('_isUnique', 'email'))
		 		);
		 **/
		
		if(isset($this->data[$this->name]['id'])) {
			//make sure that there isn't one on another id that is same as this field
			$results = $this->find($this->name.'.id<>"'.addslashes($this->data[$this->name]['id']).'" AND '.$this->name.'.'.$field.'="'.addslashes($check).'"');
		} else {
			//make sure it doesn't currently exist in the db (we're creating a new one)
			$results = $this->find($this->name.'.'.$field.'="'.addslashes($check).'"');
		}
		
		if(!empty($results))
			return false;
		else
			return true;
	}
}

?>
